package cn.iocoder.yudao.module.reservation.controller.admin.services;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.reservation.controller.admin.services.vo.*;
import cn.iocoder.yudao.module.reservation.dal.dataobject.services.ServicesDO;
import cn.iocoder.yudao.module.reservation.service.services.ServicesService;

@Tag(name = "管理后台 - 店铺服务")
@RestController
@RequestMapping("/reservation/services")
@Validated
public class ServicesController {

    @Resource
    private ServicesService servicesService;

    @PostMapping("/create")
    @Operation(summary = "创建店铺服务")
    @PreAuthorize("@ss.hasPermission('reservation:services:create')")
    public CommonResult<Long> createServices(@Valid @RequestBody ServicesSaveReqVO createReqVO) {
        return success(servicesService.createServices(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店铺服务")
    @PreAuthorize("@ss.hasPermission('reservation:services:update')")
    public CommonResult<Boolean> updateServices(@Valid @RequestBody ServicesSaveReqVO updateReqVO) {
        servicesService.updateServices(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店铺服务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('reservation:services:delete')")
    public CommonResult<Boolean> deleteServices(@RequestParam("id") Long id) {
        servicesService.deleteServices(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺服务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('reservation:services:query')")
    public CommonResult<ServicesRespVO> getServices(@RequestParam("id") Long id) {
        ServicesDO services = servicesService.getServices(id);
        return success(BeanUtils.toBean(services, ServicesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺服务分页")
    @PreAuthorize("@ss.hasPermission('reservation:services:query')")
    public CommonResult<PageResult<ServicesRespVO>> getServicesPage(@Valid ServicesPageReqVO pageReqVO) {
        PageResult<ServicesDO> pageResult = servicesService.getServicesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServicesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店铺服务 Excel")
    @PreAuthorize("@ss.hasPermission('reservation:services:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportServicesExcel(@Valid ServicesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServicesDO> list = servicesService.getServicesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店铺服务.xls", "数据", ServicesRespVO.class,
                        BeanUtils.toBean(list, ServicesRespVO.class));
    }

}